/**
 * 
 */
package Test;

import PSQueue.PSAdvertisement;
import PSQueue.PSMessage;
import PSQueue.PSPublication;
import PSQueue.PSQueue;
import PSQueue.PSSubscription;
import PSQueue.SampleSubscriber;
import Service.Variable;
import junit.framework.TestCase;

/**
 * @author cyye
 *
 */
public class PSQueueTestCase extends TestCase {

	/**
	 * @param name
	 */
	public PSQueueTestCase(String name) {
		super(name);
	}

	/* (non-Javadoc)
	 * @see junit.framework.TestCase#setUp()
	 */
	protected void setUp() throws Exception {
		super.setUp();
	}

	/* (non-Javadoc)
	 * @see junit.framework.TestCase#tearDown()
	 */
	protected void tearDown() throws Exception {
		super.tearDown();
	}
	
	public void testPSPublication() {
		PSQueue m_queue = new PSQueue();
		m_queue.testPSPublication();						
	}
		
	public void testPSSubscription() {		
		PSQueue m_queue = new PSQueue();
		m_queue.testPSSubscription();
	}
	
	public void testPSAdvertisement() {
		PSQueue m_queue = new PSQueue();
		m_queue.testPSAdvertisement();
	}
		
	public void testAddAdvertisement() {
		PSQueue m_queue = new PSQueue();
		m_queue.testAddAdvertisement();
	}
	
	public void testAddSubscription() {
		PSQueue m_queue = new PSQueue();
		m_queue.testAddSubscription();
	}
	
    public void testAddPublication() {
    	PSQueue m_queue = new PSQueue();
    	m_queue.testAddPublication();		
	}
    
    public void testQueueRunning() {
		//test plan
		//0. start queue
		//1. Advertisement		
		//2. subscription		
		//3. publish
		//4. check contents
		//5. check notification
		//6. publish
		//7. check contents
		//8. check notification
		//9. new advertisement
		//10. new subscription
		//11. publish
		//12. check contents
		//13. check notification
		
		//0.start queue
    	PSQueue m_queue = new PSQueue();
		new Thread(m_queue).start();
		
		//1. Advertisement
		String msgName1 = "msg1";	
    	String msgValue1 = "value1";
		String msgName2 = "msg2";	
		String msgValue2 = "value2";
		String msgName3 = "msg3";
		String msgValue3 = "value3";
		String msgName4 = "msg4";
		String msgValue4 = "value4";
		String msgName5 = "msg5";
		String msgValue5 = "value5";
		String msgName6 = "msg6";	
    	String msgValue6 = "value6";
		String msgName7 = "msg7";	
		String msgValue7 = "value7";
		String msgName8 = "msg8";
		String msgValue8 = "value8";
		String msgName9 = "msg9";
		String msgValue9 = "value9";
		String msgName10 = "msg10";
		String msgValue10 = "value10";
		
		PSAdvertisement adv = new PSAdvertisement();		
		
		PSMessage msg = new PSMessage();		
		msg.setMsgName(msgName1);
		msg.setMsgType(Variable.STRING);
		msg.setValue(null);
		adv.addAdvertisement(msg);
		
		msg.setMsgName(msgName2);
		msg.setMsgType(Variable.STRING);		
		adv.addAdvertisement(msg);
		m_queue.advertise(adv);
		
		adv = new PSAdvertisement();		
		
		msg.setMsgName(msgName3);
		msg.setMsgType(Variable.STRING);
		adv.addAdvertisement(msg);
		
		msg.setMsgName(msgName4);
		msg.setMsgType(Variable.STRING);
		adv.addAdvertisement(msg);
		m_queue.advertise(adv);
		
        adv = new PSAdvertisement();		
		msg.setMsgName(msgName5);
		msg.setMsgType(Variable.STRING);
		adv.addAdvertisement(msg);
		m_queue.advertise(adv);
		
		//2. subscription
		SampleSubscriber suber1 = new SampleSubscriber();
		PSSubscription sub1 = new PSSubscription();
		sub1.addID(msgName1);
		sub1.addID(msgName2);
		m_queue.subscribe(suber1, sub1); //1, 2
		
		SampleSubscriber suber2 = new SampleSubscriber();
		sub1.addID(msgName3);
		m_queue.subscribe(suber2, sub1);//1, 2, 3
		
		PSSubscription sub2 = new PSSubscription();
		sub2.addID(msgName2);
		sub2.addID(msgName4);
		SampleSubscriber suber4 = new SampleSubscriber();
		m_queue.subscribe(suber4, sub2); //2, 4
		
		SampleSubscriber suber3 = new SampleSubscriber();
		PSSubscription sub3 = new PSSubscription();
		sub3.addID(msgName3);
		sub3.addID(msgName6);		
		m_queue.subscribe(suber3, sub3); //3, 6
		
		//3. publish
		PSPublication pub1 = new PSPublication();
		PSMessage msg1 = new PSMessage();
		msg1.setMsgName(msgName1);
		msg1.setMsgType(Variable.STRING);
		msg1.setValue(msgValue1);
		pub1.addMessage(msg1);
		
		msg1.setMsgName(msgName3);
		msg1.setMsgType(Variable.STRING);
		msg1.setValue(msgValue3);
		pub1.addMessage(msg1);
		
		msg1.setMsgName(msgName4);
		msg1.setMsgType(Variable.STRING);
		msg1.setValue(msgValue4);
		pub1.addMessage(msg1);
		
		m_queue.publish(pub1);
		
		PSPublication pub2 = new PSPublication();
		msg1.setMsgName(msgName2);
		msg1.setMsgType(Variable.STRING);
		msg1.setValue(msgValue2);
		pub2.addMessage(msg1);
		
		msg1.setMsgName(msgName5);
		msg1.setMsgType(Variable.STRING);
		msg1.setValue(msgValue5);
		pub2.addMessage(msg1);
		m_queue.publish(pub2);
		
		PSPublication pub3 = new PSPublication();
		msg1.setMsgName(msgName6);
		msg1.setMsgType(Variable.STRING);
		msg1.setValue(msgValue6);
		pub3.addMessage(msg1);
		m_queue.publish(pub3);
		
		//wait 10 seconds
		try{
			Thread.sleep(10000);
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		//4, 5: check result
		//suber1, should receive 1, 2
		assert suber1.getPubNum() == 1;
		PSPublication r_pub = new PSPublication();
		PSMessage r_msg = new PSMessage();
		r_msg.setMsgName(msgName1);
		r_msg.setMsgType(Variable.STRING);
		r_msg.setValue(msgValue1);
		r_pub.addMessage(r_msg);
		
		r_msg.setMsgName(msgName2);
		r_msg.setMsgType(Variable.STRING);
		r_msg.setValue(msgValue2);
		r_pub.addMessage(r_msg);
		assert suber1.containPub(r_pub);
		
		//suber2, should receive 1, 2, 3
		assert suber2.getPubNum() == 1;
		r_msg.setMsgName(msgName3);
		r_msg.setMsgType(Variable.STRING);
		r_msg.setValue(msgValue3);
		r_pub.addMessage(r_msg);
		assert suber2.containPub(r_pub);
		
		//suber3 should receive 0
		assert suber3.getPubNum() == 0;
		
		//suber4 should receive 2, 4
		assert suber4.getPubNum() == 1;
		r_pub = new PSPublication();
		
		r_msg.setMsgName(msgName2);
		r_msg.setMsgType(Variable.STRING);
		r_msg.setValue(msgValue2);
		r_pub.addMessage(r_msg);
		
		r_msg.setMsgName(msgName4);
		r_msg.setMsgType(Variable.STRING);
		r_msg.setValue(msgValue4);
		r_pub.addMessage(r_msg);
		
		assert suber4.containPub(r_pub);
		
		//6. publish
		pub1 = new PSPublication();
		msg1 = new PSMessage();
		msg1.setMsgName(msgName1);
		msg1.setMsgType(Variable.STRING);
		msg1.setValue(msgValue5);
		pub1.addMessage(msg1);
		
		msg1.setMsgName(msgName3);
		msg1.setValue(msgValue4);
		pub1.addMessage(msg1);
		
		m_queue.publish(pub1);
		
		//wait 10 seconds
		try{
			Thread.sleep(10000);
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		//7.8 check result
		assert suber1.getPubNum() == 2;
		r_pub = new PSPublication();
		r_msg = new PSMessage();
		r_msg.setMsgName(msgName1);
		r_msg.setMsgType(Variable.STRING);
		r_msg.setValue(msgValue1);
		r_pub.addMessage(r_msg);
		
		r_msg.setMsgName(msgName2);
		r_msg.setMsgType(Variable.STRING);
		r_msg.setValue(msgValue2);
		r_pub.addMessage(r_msg);
		assert suber1.containPub(r_pub);
		
		r_pub = new PSPublication();
		r_msg = new PSMessage();
		r_msg.setMsgName(msgName1);
		r_msg.setMsgType(Variable.STRING);
		r_msg.setValue(msgValue5);
		r_pub.addMessage(r_msg);
		
		r_msg.setMsgName(msgName2);
		r_msg.setMsgType(Variable.STRING);
		r_msg.setValue(msgValue2);
		r_pub.addMessage(r_msg);
		assert suber1.containPub(r_pub);
		
		assert suber2.getPubNum() == 2;				
		r_pub = new PSPublication();
		r_msg.setMsgName(msgName1);
		r_msg.setMsgType(Variable.STRING);
		r_msg.setValue(msgValue5);
		r_pub.addMessage(r_msg);
		
		r_msg.setMsgName(msgName2);
		r_msg.setMsgType(Variable.STRING);
		r_msg.setValue(msgValue2);
		r_pub.addMessage(r_msg);
		
		r_msg.setMsgName(msgName3);
		r_msg.setMsgType(Variable.STRING);
		r_msg.setValue(msgValue4);
		r_pub.addMessage(r_msg);
		assert suber2.containPub(r_pub);
		
		//suber3 should receive 0
		assert suber3.getPubNum() == 0;
		
		//suber4 should receive 2, 4
		assert suber4.getPubNum() == 1;
		
		//9. new advertisement
        adv = new PSAdvertisement();							
		msg.setMsgName(msgName6);
		msg.setMsgType(Variable.STRING);
		msg.setValue(null);
		adv.addAdvertisement(msg);
		m_queue.advertise(adv);
		
		adv = new PSAdvertisement();							
		msg.setMsgName(msgName7);
		msg.setMsgType(Variable.STRING);
		msg.setValue(null);
		adv.addAdvertisement(msg);
		
		msg.setMsgName(msgName8);
		msg.setMsgType(Variable.STRING);
		msg.setValue(null);
		adv.addAdvertisement(msg);
		m_queue.advertise(adv);
		
		//10.new subscription
		SampleSubscriber suber5 = new SampleSubscriber();
		PSSubscription sub5 = new PSSubscription();
		sub5.addID(msgName1);
		sub5.addID(msgName7);
		m_queue.subscribe(suber5, sub5); //1, 7
		
		//11.publish
		pub1 = new PSPublication();
		msg1 = new PSMessage();
		msg1.setMsgName(msgName6);
		msg1.setMsgType(Variable.STRING);
		msg1.setValue(msgValue6);
		pub1.addMessage(msg1);
		
		msg1.setMsgName(msgName7);
		msg1.setValue(msgValue7);
		pub1.addMessage(msg1);
		
		m_queue.publish(pub1);
		
		//wait 10 seconds
		try{
			Thread.sleep(10000);
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		//12, 13. check results
		assert suber4.getPubNum() == 1;
		assert suber2.getPubNum() == 2;
		assert suber1.getPubNum() == 2;
		
		assert suber3.getPubNum() == 1;
		r_pub = new PSPublication();
		r_msg.setMsgName(msgName3);
		r_msg.setMsgType(Variable.STRING);
		r_msg.setValue(msgValue4);
		r_pub.addMessage(r_msg);
		
		r_msg.setMsgName(msgName6);
		r_msg.setMsgType(Variable.STRING);
		r_msg.setValue(msgValue6);
		r_pub.addMessage(r_msg);
		assert suber3.containPub(r_pub);
		
		assert suber5.getPubNum()==1;
		r_pub = new PSPublication();
		r_msg.setMsgName(msgName1);
		r_msg.setMsgType(Variable.STRING);
		r_msg.setValue(msgValue5);
		r_pub.addMessage(r_msg);
		
		r_msg.setMsgName(msgName7);
		r_msg.setMsgType(Variable.STRING);
		r_msg.setValue(msgValue7);
		r_pub.addMessage(r_msg);
		assert suber5.containPub(r_pub);
		
	}

}
